using System;
using UnityEngine;
using UnityEngine.UI;

namespace IQIGame.Onigao.Framework
{
    [ExecuteAlways]
    public class UIMaterialParamter : MonoBehaviour
    {
        public enum PropertyType
        {
            Float, Color, Vector
        }

        public string key;
        public PropertyType type;

        public float fValue;
        public Vector4 vValue;
        public Color cValue;

        [NonSerialized]
        private Graphic m_Graphic;

        public Graphic graphic
        {
            get { return m_Graphic ?? (m_Graphic = GetComponent<Graphic>()); }
        }

        Material Mat => graphic?.materialForRendering;

        private void Update()
        {
            if (Mat == null) return;
            if (!Mat.HasProperty(key)) return;

            switch (type)
            {
                case PropertyType.Float:
                    Mat.SetFloat(key, fValue);
                    break;
                case PropertyType.Color:
                    Mat.SetColor(key, cValue);
                    break;
                case PropertyType.Vector:
                    Mat.SetVector(key, vValue);
                    break;
            }
        }
    }
}
